package lab;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.*;

public class CenterPanel extends JPanel {

    JTable table;
    CenterPanelRear centerPanelRear = new CenterPanelRear(null);
    GridBagLayout gridBagLayout;
    GridBagConstraints c = new GridBagConstraints();
    Database database;
    EastPanel eastPanel= new EastPanel();
    String title="";
    String price="";
    String ISBN;

    public CenterPanel(CenterPanelRear centerPanelRear,EastPanel eastPanel) {
        this.centerPanelRear = centerPanelRear;
        this.eastPanel=eastPanel;
        gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);
        Object rowData[][] = {};
        Object columnNames[] = {"Title", "Author", "Genre", "ISBN", "Year", "Stock", "Price"};
        DefaultTableModel model = new DefaultTableModel(rowData, columnNames) {

            @Override
            public boolean isCellEditable(int row, int column) {
                //all cells false
                return false;
            }
        };

        model.setRowCount(0);
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        JTableHeader header = table.getTableHeader();
        scrollPane.scrollRectToVisible(null);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 10;
        c.gridx = 0;
        c.gridy = 0;
        add(scrollPane, c);
        table.getColumnModel().getColumn(0).setPreferredWidth(300);
        table.getColumnModel().getColumn(1).setPreferredWidth(300);
        table.getColumnModel().getColumn(2).setPreferredWidth(250);
        table.getColumnModel().getColumn(3).setPreferredWidth(200);
        table.getColumnModel().getColumn(4).setPreferredWidth(150);
        table.getColumnModel().getColumn(5).setPreferredWidth(150);
        table.getColumnModel().getColumn(6).setPreferredWidth(150);

        table.setShowGrid(false);
        table.setBackground(null);
        header.setBackground(null);
        header.setResizingAllowed(false);
        header.setReorderingAllowed(false);
        database = new Database(table, model, centerPanelRear);

    }

    public void searchGenreOnly(String genre) {
        switch (genre) {
            case "thriller":
                database.searchGenreOnly(genre);
                break;
            case "horror":
                database.searchGenreOnly(genre);
                break;
            case "fantasy":
                database.searchGenreOnly(genre);
                break;
            case "children":
                database.searchGenreOnly(genre);
                break;
            case "mystery":
                database.searchGenreOnly(genre);
                break;
            case "romantic":
                database.searchGenreOnly(genre);
                break;
        }
    }

    public void searchAnything(String anything) {
        database.searchAnything(anything);
    }

    private void getDescription() {
        int i = table.getSelectedRow();
        Object descObj = table.getValueAt(i, 0);
        String descStr = descObj.toString();
        centerPanelRear.textAreaDisc.setText("Description: " + database.getDescription(descStr));
    }

    private void displayRowValues(int rowIndex) {
        title = table.getValueAt(rowIndex, 0).toString();
        price = table.getValueAt(rowIndex, 6).toString() + " SEK";
        ISBN = table.getValueAt(rowIndex, 3).toString();
        getTitleAndPrice();
        getISBN();
        centerPanelRear.labelTitle.setText("Title: " + table.getValueAt(rowIndex, 0).toString());
        centerPanelRear.labelAuthor.setText("Author: " + table.getValueAt(rowIndex, 1).toString());
        centerPanelRear.labelGenre.setText("Genre: " + table.getValueAt(rowIndex, 2).toString());
        centerPanelRear.labelIsbn.setText("ISBN: " + table.getValueAt(rowIndex, 3).toString());
        centerPanelRear.labelYear.setText("Year: " + table.getValueAt(rowIndex, 4).toString());
        centerPanelRear.labelStock.setText("Stock: " + table.getValueAt(rowIndex, 5).toString() + " st");
        centerPanelRear.labelPrice.setText("Price: " + table.getValueAt(rowIndex, 6).toString() + " SEK");
        getDescription();
    }

    public String getTitleAndPrice() {
        System.out.println(title+" "+price);
        return centerPanelRear.getTitleAndPrice(title, price);
    }

    public String getISBN() {
        return centerPanelRear.getISBN(ISBN);
    }

    public void mouseClickOnTable(final Cards cards) {
        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {

                if (e.getClickCount() == 2) {
                    getDescription();
                    ListSelectionModel model = table.getSelectionModel();
                    int lead = model.getLeadSelectionIndex();
                    displayRowValues(lead);
                    cards.cl.show(cards.cardPanel, "2");
                }
            }
        });
    }
}
